<?php
namespace Phone\Controller;
use Think\Controller;
/**
 * 手机端控制器
 */
class IndexController extends Controller {
	
    public function index(){
    	//查询调用数据
    	$data = M('defaultonmodel')->field('cid,id,atitle,adigest,athum,aaddtime')->where("cid=1")->limit(5)->select();
		//重组数据  组合图片发布事件
		foreach($data as $k =>$v){
//			$data[$k]['athum'] = !$v['athum']?'noimg.jpg':$v['athum'];
			$data[$k]['aaddtime'] = date('Y-m-d H:i:s',$v['aaddtime']);		
		}
//		p($data);die;
////      $this->display();
		$this->ajaxReturn($data);
    }
	
	/**
	 * 文章页面数据请求获取
	 */
	 public function aritcleAjax(){
		$aid = I('post.aid','0','intval'); 						 					//文章id
		$cid = I('post.cid','0','intval');						 					//栏目id		
		$categoryData = M('category')->where("cid='{$cid}'")->find();		//通过cid 查询栏目模型
		$mid = $categoryData['duosute_model_mid'];
		$modelName = M("Model")->where("mid='{$mid}'")->find();
		$defaultTable = $modelName['mdefault_field'];						//默认表明
		$aloneTable = $modelName['mmodel_name'];							//独立表
		//关联表查询数据   //组合查询语句
		$join = ' join duosute_'. $aloneTable . ' on duosute_' .$defaultTable.'.id= duosute_'.$aloneTable.'.aid'; 
		$articleData = M($defaultTable)->JOIN($join)->where("aid = '{$aid}'")->find(); //查询内容

		//查询默认表内容
		$articleData = M($defaultTable)->where("id = '{$aid}'")->find(); 
		$selfData =  M($aloneTable)->where("aid = '{$aid}'")->find(); 
		if($selfData){
			foreach($selfData as $k => $v){
				$articleData[$k] = $v;
			}
		}
		
		//时间重组
		$articleData['aauthor'] = 'qianbaidu';//为做查询 赋默认值
		$articleData['aaddtime'] = date('Y-m-d H:i:s',$articleData['aaddtime']);
		$this->ajaxReturn($articleData);
	 }
	 
	 
	 
	/**
	 * 手机端请求数据api接口
	 */
	public function api(){
		$type = I('get.type');				//类型
		$limit = I('get.limit');			//条数
		$cid = I('get.cid');				//栏目id
		$mid = I('get.mid');				//模型id
	}
	
	/**
	 *栏目分类 
	 */
	 public function categoryAjax(){
	 	//查询栏目列表
	 	$model = M('model')->select();
		$category = M('category')->select();
		//实例化扩展类 栏目树
		foreach($model as $k =>$v){
			foreach($category as $kk =>$vv){
				if($vv['duosute_model_mid'] == $v['mid']){
					$model[$k]['category'][]=$vv;
				}
			}
		}	
//		p($model);
//		p($model[0]['category']);
		$this->ajaxReturn($model);
	 }
	 
	 public function listAjax(){
	 	$cid = I('post.cid',0,'intval');	 	
	 	$listData = M('defaultonmodel')->where("cid='{$cid}'")->select();		
		$this->ajaxReturn($listData);
	 }
	 
	/**
	 * 列表页数据请求
	 */
	 public function hotListAjax(){
	 	$cid = I('post.cid',0,'intval');
	 	$listData = M('defaultonmodel')->limit($cid,15)->select();
		$this->ajaxReturn($listData);
	 }
	 
	 
	/**
	 * 手机端首页数据调用
	 * 
	 */
	 public function indexAjax(){
	 	$data = M('defaultonmodel')->field('cid,id,atitle,adigest,athum,aaddtime')->where("cid=1")->limit(5)->select();
      	$this->ajaxReturn($data);
	 }
	 
	 /**
	  * 幻灯片数据请求
	  */
	  public function slideAjax(){
	  	$data = M('slide')->where("s_ismobile=1 and s_isshow = 0")->field('s_title,s_img,s_url')->select();
		$this->ajaxReturn($data);
	  }
	  
	  /**
	   * 图片模型随机数据调用
	   */
	   public function imgListAjax(){
	   		$data = M('defaultonmodel')->join('duosute_imagesonmodel on duosute_imagesonmodel.aid = duosute_defaultonmodel.id')->where('duosute_defaultonmodel.cid=12')->limit(4)->select();
			$this->ajaxReturn($data);
	   }
	   /**
	    * 历史记录处理方法
	    */
	    public function historyCid(){
	    	$cid = rtrim(ltrim($_POST['cid']));	
			$cid = array_filter(explode(',',$cid));
			foreach($cid as $k =>$v){
				$data[] = M('defaultonmodel')->where("id='{$v}'")->field('id,atitle,cid')->find();
			}
			$data= array_filter($data);
			$this->ajaxReturn($data);
	    }
	   
	   //处理用户收藏
	   public function collect(){
//	   		$aid = 1;
//	   		$uid = 1;
			$aid = I('post.aid');
			$uid = I('post.uid');
			$user = M('user')->where("uid = '{$uid}'")->field('ucollect')->find();
			$userCollect = current($user);
			$data['ucollect'] = $userCollect.','.$aid;
			M('user')->where("uid = '{$uid}'")->save($data);
			$this->ajaxReturn(1);
	   }
	   
	   //读取用户收藏列表
	   public function userCollect(){
	   		$uid = I('post.uid');
			$user = M('user')->where("uid = '{$uid}'")->field('ucollect')->find();
			$userCollect = $user['ucollect'];
			$userCollect = array_filter(explode(',',$userCollect));
			foreach($userCollect as $k => $v){
				$data[] = M('defaultonmodel')->where("id='{$v}'")->field('id,atitle,cid')->find();
			} 
			$data= array_filter($data);
			$this->ajaxReturn($data);
	   }
	   
	   
}